import { wxRequest } from "../../utils/httpRequest"
// import initComputed from 'wx-computed'
import utils from "../../utils/util"
import Notify from '@vant/weapp/notify/notify';

Page({
    data: {
      inputSearchValue: '',
      orderList: null,
      evaluateId: "",                   // 正在评分的orderId
      showRateDialogFlag: false,        // 打分的dialogFlag
      rateValue: 0,                     // 打分的分数
      showEvaluateDialogFlag: false,    // 评价的dialogFlag
      evaluateValue: ""                 // 评价的value
    },
    getAllOrder() {
        wxRequest('getAllOrder', {}).then((res) => {
            let list = res.data.map((item) => {
                let itemNum = 0;
                item.orderItems.forEach((itm) => {
                    itemNum = itemNum + itm.num;
                });
                item.allItemNum = itemNum;
                return item;
            });
            this.setData({ orderList: list });
        });
    },
    goShop(event) {
        wx.navigateTo({
            url: `/home_package/shopMeals/shopMeals?shopId=${event.currentTarget.dataset.shopid}`,
        })
    },
    goOrderDetail(event) {
        wx.navigateTo({
            url: `/order_package/orderDetail/orderDetail?orderId=${event.currentTarget.dataset.orderid}`,
        })
    },
    startShowRateDialog(event) {
        let orderItem = this.data.orderList.filter((item) =>item.orderId == event.currentTarget.dataset.orderid)[0];
        if(!orderItem.rate) {
            this.setData({ evaluateId: event.currentTarget.dataset.orderid });
            this.setData({ showRateDialogFlag: true });
        } else {
            if(!orderItem.evaluate) {
                this.setData({ evaluateId: event.currentTarget.dataset.orderid });
                this.setData({ showEvaluateDialogFlag: true });
            } else {
                Notify({ type: 'warning', message: '您已经对该订单评价过了' });
            }
        }
    },
    onRateChange(event) {
        this.setData({ rateValue: event.detail });
    },
    rateClose() {
        this.setData({ rateValue: 0 });
        this.setData({ showRateDialogFlag: false });
    },
    doRate() {
        let url = "doRate";
        let params = {
            orderId: this.data.evaluateId,
            rate: this.data.rateValue
        }
        wxRequest(url, params).then((res) => {
            if(res.data == "成功") {
                this.setData({ showRateDialogFlag: false });
                this.setData({ showEvaluateDialogFlag: true });
            }
        });
    },
    evaluateClose() {
        this.setData({ evaluateValue: "" });
        this.setData({ showEvaluateDialogFlag: false });
        this.getAllOrder();
    },
    // 评价输入框的变换
    fieldValueChange(event) {
        this.setData({ evaluateValue: event.detail });
    },
    doEvaluate() {
        let url = "doEvaluate";
        let params = {
            orderId: this.data.evaluateId,
            evaluate: this.data.evaluateValue
        }
        wxRequest(url, params).then((res) => {
            if(res.data == "成功") {
                this.setData({ showEvaluateDialogFlag: false });
                Notify({ type: 'success', message: '评价成功' });
                this.getAllOrder();
            }
        });
    },
    onLoad(options) {
    },
    onShow() {
        this.getAllOrder();
    }
})